var study_current_page = 0;
var firstStudyLoad = false;

function searchStudy(q, start,records){
    //console.log('searching studies');
    if(q != ''){
        var data = {
            q:q, 
            start:start, 
            records:records,
            format:'json'
        };

        var lang = $("#select-language").val();
        if(lang != 'all')
            data['lang'] = lang;

        if($("#series").val())
            data['series'] = $("#series").val().join(',');

        if(current_lang.length != 0){
            data.lang = current_lang;
        }
        $.ajax({
            url: backend+'/ws/search.xql',
            dataType: 'jsonp',
            data: data,
            success: studySearchDone
        });
        $('.study-tab').removeClass('loading');
    }else{
        $("#study-hits-count").html('');
        $("#study-result").html('');
        $("#study-pagination").hide();		
    }
}

function studySearchDone(data){
    if(parseInt(data.hits) > 0){
        $("#study-hits-count").html('('+data.hits+')');
        $("#study-pagination").show();

        //console.log('study results: '+data.hits);

        var output = '';
        if(data.StudyUnit instanceof Array){

            $.each(data['StudyUnit'], function(i,study){
                //console.log('study loop ' + i);
                output +=  renderStudy(study);
            });
        }else{
            output +=  renderStudy(data['StudyUnit']);
        }
        $("#study-result").html(output);

        //$(".question-block p").hide();
        //$(".question-block ."+$("#select-language").val()).show();
        //if this is the first result-set

    }else{
        $("#study-hits-count").html('');
        $("#study-result").html('');
        $("#study-pagination").hide();
    }
    $('.study-tab a').removeClass('loading');
}

function renderStudy(study){
    var output = '';
    
    output += $.format('<div id="%s" class="question-block">', [study.id]);
    
    output += '<h4 class="lang-strings">';
    $.each(study.title, function(lang, title){
        output += $.format('<span class="%s"><a href="http://snd.gu.se/%s/catalogue/study/%s">%s</a></span>', [lang, lang, study.id, title]);
    });    
    output += '</h4><br/>';
    
    if(study['abstract'] != null){
        output += '<p class="lang-strings">';
        $.each(study['abstract'], function(lang, abs){
            output += $.format('<span class="%s">%s</span>', [lang, abs.ellipsisText('end', 150)]);
        });
        output += '</p><br/>';
    }
    
    if(study.questions > 0)
        output += $.format('<span class="question-icon">:%s</span>', [study.questions]);
    if(study.variables > 0)
        output += $.format('<span class="variable-icon">:%s</span>',  [study.variables]);
    output += '<p><a class="marc" target="_blank" href="#">marc</a> <a class="pdf" target="_blank" href="#">pdf</a> <a class="ddi" target="_blank" href="#">ddi</a></p>';
        
    output += '</div>';
    return output;
}

